package cn.office.enums.file;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;


import java.lang.reflect.Method;
import java.util.Arrays;

@AllArgsConstructor
@Slf4j
public final class FileContentTypeEnums {


    @Getter
    @AllArgsConstructor
    public enum excelTypeEnum implements FileContentTypeEnumApi {
        XLSX("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"),
        XLS("xls", "application/vnd.ms-excel");

        // 文件扩展名
        private String fileExtension;
        // 文件contentType
        private String contentType;
    }

    @Getter
    @AllArgsConstructor
    public enum pdfTypeEnum implements FileContentTypeEnumApi {
        PDF("pdf", "application/pdf");

        // 文件扩展名
        private String fileExtension;
        // 文件contentType
        private String contentType;
    }

    @Getter
    @AllArgsConstructor
    public enum wordTypeEnum implements FileContentTypeEnumApi {
        DOC("doc", "application/msword"),
        DOCX("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");

        // 文件扩展名
        private String fileExtension;
        // 文件contentType
        private String contentType;
    }

    public static String getContentTypeByFileExtension(String fileExtensionParam) {
        String contentTypeResult = null;
        Class<?> clazz = null;
        try {
            /**
             * {@link cn.office.enums.file.FileContentTypeEnums}
             */
            // 反射获取FileContentTypeEnums
            clazz = Class.forName("cn.office.enums.file.FileContentTypeEnums");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 获取下面所有的枚举类
        Class<?>[] classes = clazz.getClasses();
        for (Class<?> aClass : classes) {
            if(StringUtils.isNotEmpty(contentTypeResult)){
                break;
            }
            // 获取枚举类的所有枚举值
            Object[] enumConstants = aClass.getEnumConstants();
            // 只拿枚举
            if (CollectionUtils.isNotEmpty(Arrays.asList(enumConstants))) {
                for (Object enumConstant : enumConstants) {
                    Method getCode;
                    Method getContentType;
                    String fileExtension = null;
                    String contentType = null;
                    try {
                        getCode = aClass.getMethod("getFileExtension");
                        fileExtension = String.valueOf(getCode.invoke(enumConstant));
                    } catch (Exception e) {
                        log.error("获取枚举类的getCode方法失败", e);
                    }
                    try {
                        getContentType = aClass.getMethod("getContentType");
                        contentType = String.valueOf(getContentType.invoke(enumConstant));
                    } catch (Exception e) {
                        log.error("获取枚举类的getContentType方法失败", e);
                    }
                    if (fileExtension.equals(fileExtensionParam)) {
                        contentTypeResult = contentType;
                        break;
                    }
                }
            }
        }
        return contentTypeResult;
    }
}
